package com.vincent.kdp.ui.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.vincent.kdp.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 0704 on 2016/9/21.
 */
public class CommodityPagerAdapter extends PagerAdapter {
    private Context mContext;
    private List<ImageView> mImageViewList;
    private List<String> mNewCommodityList;
    private OnItemClicker mOnItemClicker;

    public CommodityPagerAdapter(Context context, List<String> mNewCommodityList) {
        mContext = context;
        mImageViewList = new ArrayList<>();
        this.mNewCommodityList = mNewCommodityList;
        for (int i = 0; i < mNewCommodityList.size(); i++) {
            ImageView imageView = new ImageView(mContext);
            mImageViewList.add(imageView);
        }
    }

    @Override
    public int getCount() {
        return mImageViewList.size();

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ImageView imageView = mImageViewList.get(position);

        Glide.with(mContext).load(mNewCommodityList.get(position)).placeholder(R.mipmap.zikjlogo).into(imageView);
        container.addView(imageView);


        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClicker != null) {
                    mOnItemClicker.onClick(mNewCommodityList.get(position), position);
                }
            }
        });
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mImageViewList.get(position));
    }

    public void setOnItemClicker(OnItemClicker onItemClicker) {
        mOnItemClicker = onItemClicker;
    }

    public interface OnItemClicker{
        void onClick(String imageUrl, int position);
    }
}
